package com.gzhryc.html.impls;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import com.gzhryc.html.IHtmlThemeService;
import com.gzhryc.html.models.HtmlTemplate;
import com.gzhryc.html.models.HtmlTheme;

public class MemoryHtmlThemeService implements IHtmlThemeService{
	
	ConcurrentHashMap<String,HtmlTheme> htmlThemeStore;
	
	public MemoryHtmlThemeService() {
		this.htmlThemeStore = new ConcurrentHashMap<String,HtmlTheme>();
	}

	@Override
	public List<String> findByHtmlThemeCodes() {
		List<String> result = new ArrayList<String>(htmlThemeStore.keySet());
		return result;
	}

	@Override
	public HtmlTheme getByCode(String code) {
		return this.htmlThemeStore.get(code);
	}

	@Override
	public List<HtmlTemplate> findHtmlTemplateByCode(String code) {
		List<HtmlTemplate> result = null;
		HtmlTheme htmlTheme = this.htmlThemeStore.get(code);
		if(htmlTheme != null) {
			result = new ArrayList<HtmlTemplate>(htmlTheme.getTemplates().values());
		}
		return result;
	}

	@Override
	public void addHtmlTheme(HtmlTheme htmlTheme) {
		if(htmlTheme != null) {
			this.htmlThemeStore.put(htmlTheme.getCode(), htmlTheme);
		}
	}

	@Override
	public void addHtmlTemplate(String code, HtmlTemplate template) {
		HtmlTheme htmlTheme = this.htmlThemeStore.get(code);
		if(htmlTheme != null) {
			htmlTheme.getTemplates().put(template.getHtmlPath(), template);
		}
	}

}
